package com.oops.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.UUID;

@Component
@Slf4j
public class RabbitMQSender {
    private final RabbitTemplate rabbitTemplate;
    private final MessageConverter messageConverter;

    final ObjectMapper objectMapper = new ObjectMapper();

    public RabbitMQSender(RabbitTemplate rabbitTemplate, MessageConverter messageConverter) {
        this.messageConverter = messageConverter;
        this.rabbitTemplate = rabbitTemplate;
        final ConnectionFactory connectionFactory = rabbitTemplate.getConnectionFactory();
        try {
            // final String s = objectMapper.writeValueAsString(connectionFactory);
            log.info("connectionFactory {}",connectionFactory);
        }catch (Exception e) {
            e.printStackTrace();
        }
        this.rabbitTemplate.setMessageConverter(messageConverter);
    }


    public void send(String exchange, String routingKey, Object object) {
        // 如果消息没有到exchange,则confirm回调,ack=false
        // 如果消息到达exchange,则confirm回调,ack=true
        // rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
        //     assert correlationData != null;
        //     if (ack) {
        //         log.info("发送消息到交换器成功，ID为:{}  返回消息为:{}", correlationData.getId(), Objects.requireNonNull(correlationData.getReturned()));
        //     } else {
        //         log.info("发送消息到交换器失败，ID为:{}，原因是:{}", correlationData.getId(), cause);
        //
        //     }
        // });
        // // exchange到queue成功,则不回调return
        // // exchange到queue失败,则回调return(需设置mandatory=true,否则不回回调,消息就丢了)
        // rabbitTemplate.setReturnsCallback(returned -> log.info("消息路由到队列的结果 returned : {}", returned));

        log.info("send Thread.currentThread().getName() -> {}",Thread.currentThread().getName());

        final CorrelationData correlationData = new CorrelationData();
        correlationData.setId(UUID.randomUUID().toString());
        correlationData.setReturned(new ReturnedMessage(new Message("success".getBytes(StandardCharsets.UTF_8)),200,"ok",routingKey,exchange));
        rabbitTemplate.convertAndSend(exchange, routingKey+"1", object, correlationData);
        // final CorrelationData correlationData1 = new CorrelationData();
        // correlationData1.setId(UUID.randomUUID().toString());
        // rabbitTemplate.convertAndSend(exchange, routingKey, object, correlationData1);

    }
}
